package com.bambooshoot.service;


import com.bambooshoot.common.core.page.PageQuery;
import com.bambooshoot.common.core.page.TableDataInfo;
import com.bambooshoot.common.utils.MapstructUtils;
import com.bambooshoot.common.utils.StringUtils;
import com.bambooshoot.domain.BizIncomeYearlySummary;
import com.bambooshoot.domain.bo.BizIncomeYearlySummaryBo;
import com.bambooshoot.domain.vo.BizIncomeYearlySummaryVo;
import com.bambooshoot.mapper.BizIncomeYearlySummaryMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 年收入汇总Service业务层处理
 *
 * @author mxc
 * @date 2024-07-09
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BizIncomeYearlySummaryService extends ServiceImpl<BizIncomeYearlySummaryMapper, BizIncomeYearlySummary> {


    /**
     * 查询年收入汇总
     *
     * @param id 主键
     * @return 年收入汇总
     */
    public BizIncomeYearlySummaryVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询年收入汇总列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 年收入汇总分页列表
     */
    public TableDataInfo<BizIncomeYearlySummaryVo> queryPageList(BizIncomeYearlySummaryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BizIncomeYearlySummary> lqw = buildQueryWrapper(bo);
        Page<BizIncomeYearlySummaryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的年收入汇总列表
     *
     * @param bo 查询条件
     * @return 年收入汇总列表
     */
    public List<BizIncomeYearlySummaryVo> queryList(BizIncomeYearlySummaryBo bo) {
        LambdaQueryWrapper<BizIncomeYearlySummary> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BizIncomeYearlySummary> buildQueryWrapper(BizIncomeYearlySummaryBo bo) {
        LambdaQueryWrapper<BizIncomeYearlySummary> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getIncomeYear() != null, BizIncomeYearlySummary::getIncomeYear, bo.getIncomeYear());
        lqw.eq(bo.getAmount() != null, BizIncomeYearlySummary::getAmount, bo.getAmount());
        lqw.ge(bo.getMinAmount() != null, BizIncomeYearlySummary::getAmount, bo.getMinAmount());
        lqw.le(bo.getMaxAmount() != null, BizIncomeYearlySummary::getAmount, bo.getMaxAmount());
        lqw.like(StringUtils.isNotBlank(bo.getRemark()), BizIncomeYearlySummary::getRemark, bo.getRemark());
        lqw.orderByDesc(BizIncomeYearlySummary::getIncomeYear);
        return lqw;
    }

    /**
     * 新增年收入汇总
     *
     * @param bo 年收入汇总
     * @return 是否新增成功
     */
    public Boolean insertByBo(BizIncomeYearlySummaryBo bo) {
        BizIncomeYearlySummary add = MapstructUtils.convert(bo, BizIncomeYearlySummary.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改年收入汇总
     *
     * @param bo 年收入汇总
     * @return 是否修改成功
     */
    public Boolean updateByBo(BizIncomeYearlySummaryBo bo) {
        BizIncomeYearlySummary update = MapstructUtils.convert(bo, BizIncomeYearlySummary.class);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 校验并批量删除年收入汇总信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 保存完明细数据后，处理报表数据
     */
    @Async
    public void handleDataAfterSave() {
        handleYearlyIncomeSummary();
    }

    /**
     * 计算年收入报表数据
     */
    public void handleYearlyIncomeSummary() {
        baseMapper.handleIncomeYearlySummary();
        baseMapper.deleteInvalidData();
    }
}
